package com.minishift.android.init;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;

/**
 * 
 * this class will implement Init class.
 * @author Sirier
 *
 */
public class InitAction {

	private static InitAction instance; 
	
	public InitAction(){};
	
	/**
	 * this method will get only object of InitAction.
	 * @return InitAction: only object of InitAction
	 */
	public static InitAction getInstance(){
		if(null  == instance){
			instance = new InitAction();
		}
		return instance;
	}
	
	/**
	 * this method will initialize Activity and Widget.
	 * @param activity: will be initialized Activity.
	 */
	public void init(Activity activity){
		Init i = activity.getClass().getAnnotation(Init.class);
		if(i != null){
			initActivity(activity, i);
		}
		Field[] fields = activity.getClass().getDeclaredFields();
		if(null != fields && fields.length >0){
			for(Field field:fields){
				if(field.isAnnotationPresent(Init.class)){
					initWidgets(activity, field);
				}
			}		
		}
	}
	
	/**
	 * this method will initialize Widget of Activity.
	 * @param activity: Widget belong to Activity.
	 * @param field: Filed object.
	 */
	private void initWidgets(Activity activity, Field field){
		Init init = field.getAnnotation(Init.class);
		try {
			field.setAccessible(true);
			field.set(activity, activity.findViewById(init.id()));
			if(init.onClick()){
				Method m = field.getType().getMethod("setOnClickListener", OnClickListener.class);
				m.invoke(activity.findViewById(init.id()), activity);	
			}
			if (init.useFont()) {
				String[] files = activity.getAssets().list("");
				for (String file : files) {
					if (file.equals("fonts")) {
						String[] fonts = activity.getAssets().list("fonts");
						for (String font : fonts) {
							if (font.contains(".ttf")) {
								Typeface tf = Typeface.createFromAsset(
										activity.getAssets(), "fonts/" + font);
								Method f = field.getType().getMethod(
										"setTypeface", Typeface.class);
								f.invoke(activity.findViewById(init.id()), tf);
								return;
							}
						}
					}
				}
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * this method will initialize the Activity
	 * @param activity	will be initialized activity.
	 * @param i	Init object.
	 */
	private void initActivity(Activity activity, Init i){
		if(i.noTitle())
			activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
		activity.setContentView(i.id());
		if(i.fullScreen()){
			activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
	}
}
